package com.nicho.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import com.nicho.pojo.Course;
import com.nicho.pojo.SRoot;
import com.nicho.pojo.Student;
import com.nicho.pojo.Teacher;
import com.nicho.util.CloseStream;
import com.nicho.util.MD5;

/*
 * @Author: 姜东浩
 * @Date: 2021-11-08 17:26:58
 * @LastEditors: 姜东浩
 * @LastEditTime: 2021-11-09 11:35:37
 * @Description: 超级管理员数据库操作
 */
public class SRootDAO {

    static SRoot s = null;
    static File studentData = new File("src/com/nicho/file/StudentData.txt");
    static File sRootData = new File("src/com/nicho/file/SRootData.txt");
    static File courseData = new File("src/com/nicho/file/CourseData.txt");
    static File teacherData = new File("src/com/nicho/file/TeacherData.txt");

    /**
     * @description:初始化一位超级管理员,密码通过MD5加密
     * @return 管理员对象
     * @author: 姜东浩
     */
    public SRoot initSRoot() {

        return new SRoot("r10001", MD5.md5("root"));

    }

    /**
     * @description:超级管理员登录，登录后，静态变量s将为从文件读取出来的超级管理员，供全局使用
     * @param sRoot 被检查的管理员对象
     * @return 管理员对象
     * @author: 姜东浩
     */
    public SRoot sRootLogin(SRoot sRoot) {

        ObjectInputStream ois = null;
        // 如果存储超级管理员的文件不为空，则反序列化，否则返回null
        try {
            if (sRootData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(sRootData));
                s = (SRoot) ois.readObject();
            } else {
                System.out.println("文件为空");
                return null;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }
        // 如果传进来的账号密码与文件中保存的账号密码相等，则返回管理员对象，否则返回null
        if (sRoot.getsRootId().equals(s.getsRootId()) && MD5.md5(sRoot.getPassword()).equals(s.getPassword())) {
            return s;
        } else {
            return null;
        }

    }

    // ========================================================================================================================
    /**
     * @description:根据学号判断数据库中是否有此的学生对象
     * @param student 待判断的学生对象
     * @return 数据库中的学生对象或null
     * @author: 姜东浩
     */
    public Student existStudent(Student student) {

        ObjectInputStream ois = null;
        // 如果存学生的文件不为空，则反序列化，否则返回-1
        try {
            if (studentData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(studentData));
                s.setAllStudentList((ArrayList<Student>) ois.readObject());
            } else {
                return null;
            }
            // 将传进来的学生对象的账号与文件保存的学生账号进行比较，相等返回1，不相等返回-1
            for (int i = 0; i < s.getAllStudentList().size(); i++) {
                if (s.getAllStudentList().get(i).getStudentId().equals(student.getStudentId())) {
                    return s.getAllStudentList().get(i);
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }
        return null;

    }

    /**
     * @description:向数据库中添加一名学生
     * @param student 待添加的学生
     * @return 添加成功返回1，失败返回-1
     * @author: 姜东浩
     */
    public int insertOneStudent(Student student) {

        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存学生的文件不为空，则反序列化后传给s里的AllStudentList，否则新建一个List
        try {
            if (studentData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(studentData));
                s.setAllStudentList((ArrayList<Student>) ois.readObject());
            } else {
                s.setAllStudentList(new ArrayList<Student>());
            }
            // 向List集合添加一条数据，序列化到文件中，成功返回1，失败返回-1
            s.getAllStudentList().add(student);
            oos = new ObjectOutputStream(new FileOutputStream(studentData));
            oos.reset();
            oos.writeObject(s.getAllStudentList());
            return 1;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }

    }

    /**
     * @description: 在数据库中删除一名学生
     * @param {Long} studentId
     * @return 删除成功返回1，失败返回-1
     * @author: 姜东浩
     */
    public int deleteOneStudentInASL(String studentId) {

        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存学生的文件不为空，则反序列化后传给s里的AllStudentList，否则返回-1
        try {
            if (studentData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(studentData));
                s.setAllStudentList((ArrayList<Student>) ois.readObject());
            } else {
                return -1;
            }
            // 遍历学生列表，找到后删除，再把更新完的List序列化到文件，返回1，没找到返回-1
            for (int i = 0; i < s.getAllStudentList().size(); i++) {
                if (s.getAllStudentList().get(i).getStudentId().equals(studentId)) {
                    s.getAllStudentList().remove(i);
                    oos = new ObjectOutputStream(new FileOutputStream(studentData));
                    oos.reset();
                    oos.writeObject(s.getAllStudentList());
                    return 1;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }
        return -1;

    }

    /**
     * @description: （该教师被删除了，但课程还在，所以只用设置讲师为null即可，不必删除课程）将改门课程的讲师在所有学生课程List中设为null
     * @param {String} courseId
     * @return 成功返回1，没有学生数据库-1，异常返回-3
     * @author: 姜东浩
     */
    public int setOneCourseTeacherNULL(String courseId) {
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存学生的文件不为空，则反序列化后传给s里的AllStudentList，否则返回-1
        try {
            if (studentData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(studentData));
                s.setAllStudentList((ArrayList<Student>) ois.readObject());
            } else {
                return -1; // 没有学生数据库，不用删
            }
            // 遍历学生列表，找到后移除所这门课程，返回1，没找到返回-1
            for (int i = 0; i < s.getAllStudentList().size(); i++) {
                for (int j = 0; j < s.getAllStudentList().get(i).getsCourseList().size(); j++) {
                    if (s.getAllStudentList().get(i).getsCourseList().get(j).getCourseId().equals(courseId)) {
                        s.getAllStudentList().get(i).getsCourseList().get(j).setcTeacher(null);
                        break;
                    }
                }
            }
            oos = new ObjectOutputStream(new FileOutputStream(studentData));
            oos.reset();
            oos.writeObject(s.getAllStudentList());
            return 1;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -3; // 异常返回-3
        } catch (IOException e) {
            e.printStackTrace();
            return -3; // 异常返回-3
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }
    }

    /**
     * @description: 从学生课程集合移除一门课程
     * @param {String} teacherId
     * @return 成功返回1，没有学生数据库-1，异常返回-3
     * @author: 姜东浩
     */
    public int removeCourseInsCourseList(Course course) {
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存学生的文件不为空，则反序列化后传给s里的AllStudentList，否则返回-1
        try {
            if (studentData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(studentData));
                s.setAllStudentList((ArrayList<Student>) ois.readObject());
            } else {
                return -1; // 没有学生数据库，不用删
            }
            // 遍历学生列表，找到后移除所这门课程，返回1，没找到返回-1
            for (int i = 0; i < s.getAllStudentList().size(); i++) {
                for (int j = 0; j < s.getAllStudentList().get(i).getsCourseList().size(); j++) {
                    if (s.getAllStudentList().get(i).getsCourseList().get(j).getCourseId()
                            .equals(course.getCourseId())) {
                        s.getAllStudentList().get(i).getsCourseList().remove(j);
                    }
                }
            }
            oos = new ObjectOutputStream(new FileOutputStream(studentData));
            oos.reset();
            oos.writeObject(s.getAllStudentList());
            return 1;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -3; // 异常返回-3
        } catch (IOException e) {
            e.printStackTrace();
            return -3; // 异常返回-3
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }

    }

    /**
     * @description: 查找指定课程有多少学生选
     * @param {Course} course
     * @return 没有异常返回选了此门课程的学生集合，有异常返回null
     * @author: 姜东浩
     */
    public ArrayList<Student> findOneCourseInALLSCourseList(Course course) {

        ObjectInputStream ois = null;
        ArrayList<Student> students = new ArrayList<Student>();
        // 如果存学生的文件不为空，则反序列化后传给s里的AllStudentList，否则返回null
        try {
            if (studentData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(studentData));
                s.setAllStudentList((ArrayList<Student>) ois.readObject());
            } else {
                return students; // 没有学生数据库，不用遍历
            }
            // 遍历学生列表，该学生有对应的课程，把此学生添加到List中最后返回List
            for (int i = 0; i < s.getAllStudentList().size(); i++) {
                for (int j = 0; j < s.getAllStudentList().get(i).getsCourseList().size(); j++) {
                    if (s.getAllStudentList().get(i).getsCourseList().get(j).getCourseId()
                            .equals(course.getCourseId())) {
                        students.add(s.getAllStudentList().get(i));
                    }
                }
            }
            return students; // 遍历完返回List
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null; // 异常返回null
        } catch (IOException e) {
            e.printStackTrace();
            return null; // 异常返回null
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }

    }

    /**
     * @description: 修改学生密码
     * @param {Long}   studentId 学生账号
     * @param {String} password 更改后的密码
     * @return 修改成功返回1，失败返回-1
     * @author: 姜东浩
     */
    public int changeStudentPwd(String studentId, String password) {

        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存学生的文件不为空，则反序列化后传给s里的AllStudentList，否则返回-1
        try {
            if (studentData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(studentData));
                s.setAllStudentList((ArrayList<Student>) ois.readObject());
            } else {
                return -1;
            }
            // 遍历学生列表，找到后修改密码，再把更新完的List序列化到文件，返回1，没找到返回-1
            for (int i = 0; i < s.getAllStudentList().size(); i++) {
                if (s.getAllStudentList().get(i).getStudentId().equals(studentId)) {
                    s.getAllStudentList().get(i).setPassword(password);
                    oos = new ObjectOutputStream(new FileOutputStream(studentData));
                    oos.reset();
                    oos.writeObject(s.getAllStudentList());
                    return 1;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }
        return -1;

    }

    /**
     * @description: 查看所有学生列表
     * @return 学生列表
     * @author: 姜东浩
     */
    public ArrayList<Student> showALlStudents() {

        ObjectInputStream ois = null;
        // 如果存学生的文件不为空，则反序列化后传给s里的AllStudentList，否则新建一个List传给s
        try {
            if (studentData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(studentData));
                s.setAllStudentList((ArrayList<Student>) ois.readObject());
            } else {
                s.setAllStudentList(new ArrayList<Student>());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }
        return s.getAllStudentList();

    }

    // ========================================================================================================================
    /**
     * @description: 通过教师对象判断是否有此Id的教师
     * @param {Teacher} teacher 带比较的教师对象
     * @return 有返回数据库中的此对象，没有返回null
     * @author: 姜东浩
     */
    public Teacher existTeacher(Teacher teacher) {

        ObjectInputStream ois = null;
        // 如果存教师的文件不为空，则反序列化后传给s里的AllTeacherList，否则新建一个List传给s
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                s.setAllTeacherList((ArrayList<Teacher>) ois.readObject());
            } else {
                return null;
            }
            // 遍历List，找到后返回1，否则返回-1
            for (int i = 0; i < s.getAllTeacherList().size(); i++) {
                if (s.getAllTeacherList().get(i).getTeacherId().equals(teacher.getTeacherId())) {
                    return s.getAllTeacherList().get(i);
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }
        return null;

    }

    /**
     * @description: 通过教师ID判断是否有此Id的教师
     * @param {Teacher} teacher 带比较的教师对象
     * @return 有返回数据库中的此对象，没有返回null
     * @author: 姜东浩
     */
    public Teacher existTeacherById(String teacherId) {

        ObjectInputStream ois = null;
        // 如果存教师的文件不为空，则反序列化后传给s里的AllTeacherList，否则新建一个List传给s
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                s.setAllTeacherList((ArrayList<Teacher>) ois.readObject());
            } else {
                return null;
            }
            // 遍历List，找到后返回1，否则返回-1
            for (int i = 0; i < s.getAllTeacherList().size(); i++) {
                if (s.getAllTeacherList().get(i).getTeacherId().equals(teacherId)) {
                    return s.getAllTeacherList().get(i);
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }
        return null;

    }

    /**
     * @description: 添加一名教师
     * @param {Teacher} teacher
     * @return 成功返回1，失败返回-1
     * @author: 姜东浩
     */
    public int insertOneTeacher(Teacher teacher) {

        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存教师的文件不为空，则反序列化后传给s里的AllTeacherList，否则新建一个List
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                s.setAllTeacherList((ArrayList<Teacher>) ois.readObject());
            } else {
                s.setAllTeacherList(new ArrayList<Teacher>());
            }
            // 向List集合添加一条数据，序列化到文件中，成功返回1，失败返回-1
            s.getAllTeacherList().add(teacher);
            oos = new ObjectOutputStream(new FileOutputStream(teacherData));
            oos.reset();
            oos.writeObject(s.getAllTeacherList());
            return 1;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }

    }

    /**
     * @description: 在教师总表中删除一名教师
     * @param {String} teacherId
     * @return 删除成功返回1，失败返回-1
     * @author: 姜东浩
     */
    public int deleteOneTeacherInATL(String teacherId) {

        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存教师的文件不为空，则反序列化后传给s里的AllTeacherList，否则返回-1
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                s.setAllTeacherList((ArrayList<Teacher>) ois.readObject());
            } else {
                return -1;
            }
            // 遍历教师列表，找到后删除，再把更新完的List序列化到文件，返回1，没找到返回-1
            for (int i = 0; i < s.getAllTeacherList().size(); i++) {
                if (s.getAllTeacherList().get(i).getTeacherId().equals(teacherId)) {
                    s.getAllTeacherList().remove(i);
                    oos = new ObjectOutputStream(new FileOutputStream(teacherData));
                    oos.reset();
                    oos.writeObject(s.getAllTeacherList());
                    return 1;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }
        return -1;

    }

    /**
     * @description: 在教师列表删除一名学生
     * @param {String} studentId
     * @return 删除成功返回1，没有教师表不用删除返回-1，没在教师表中找到返回-2，出错返回-3
     * @author: 姜东浩
     */
    public int deleteOneStudentInTSL(String studentId) {

        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存教师的文件不为空，则反序列化后传给s里的AllTeacherList，否则返回-1
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                s.setAllTeacherList((ArrayList<Teacher>) ois.readObject());
            } else {
                return -1;// 没有教师表
            }
            // 遍历教师列表，从中找有没有此学生
            // 找到后删除，再把更新完的List序列化到文件，返回1，没找到返回-2
            for (int i = 0; i < s.getAllTeacherList().size(); i++) {
                for (int j = 0; j < s.getAllTeacherList().get(i).getStudentList().size(); j++) {
                    if (s.getAllTeacherList().get(i).getStudentList().get(j).getStudentId().equals(studentId)) {
                        s.getAllTeacherList().get(i).getStudentList().remove(j);
                    }
                }
            }
            oos = new ObjectOutputStream(new FileOutputStream(teacherData));
            oos.reset();
            oos.writeObject(s.getAllTeacherList());
            return 1;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -3;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return -3;
        } catch (IOException e) {
            e.printStackTrace();
            return -3;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }

    }

    /**
     * @description: 将对应教师所教课程的讲师设为空（删除一名教师后，他教的课程不应该删除，但讲师应该设为空，等待管理员添加讲师）
     * @param {String} teacherId
     * @return 设置成功返回1，没有课程列表返回-1，没找到该教师所教的课程返回-2，抛出异常返回-3
     * @author: 姜东浩
     */
    public int deleteOneTeacherInCT(String teacherId) {

        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存教师的文件不为空，则反序列化后传给s里的AllTeacherList，否则返回-1
        try {
            if (courseData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(courseData));
                s.setAllCourseList((ArrayList<Course>) ois.readObject());
            } else {
                return -1; // 没有课程数据库，用删除
            }
            // 遍历课程列表，找到后删除，再把更新完的List序列化到文件，返回1，没找到返回-1
            for (int i = 0; i < s.getAllCourseList().size(); i++) {
                if(s.getAllCourseList().get(i).getcTeacher()==null){
                    return 1;
                }
                if (s.getAllCourseList().get(i).getcTeacher().getTeacherId().equals(teacherId)) {
                    s.getAllCourseList().get(i).setcTeacher(null);
                    oos = new ObjectOutputStream(new FileOutputStream(courseData));
                    oos.reset();
                    oos.writeObject(s.getAllCourseList());
                    return 1;
                }
            }
            return -2;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -3;
        } catch (IOException e) {
            e.printStackTrace();
            return -3;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }

    }

    /**
     * @description: 判断此老师是否在教课
     * @param {String} teacherId
     * @return 在教课返回1，不存在教师列表或没找到该老师返回-1，抛异常返回-2
     * @author: 姜东浩
     */
    public int existTeacherHasCourse(String teacherId) {

        ObjectInputStream ois = null;
        // 如果存教师的文件不为空，则反序列化后传给s里的AllTeacherList，否则返回-1
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                s.setAllTeacherList((ArrayList<Teacher>) ois.readObject());
            } else {
                return -1;
            }
            // 遍历教师列表，找到后查看他是否在教课，在返回1，不在返回-2
            for (int i = 0; i < s.getAllTeacherList().size(); i++) {
                if (s.getAllTeacherList().get(i).getTeacherId().equals(teacherId)) {
                    if (s.getAllTeacherList().get(i).gettCourseId() != null) {
                        return 1;
                    } else {
                        return -2;
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }
        return -1;

    }

    /**
     * @description: （新课）通过教师Id和课程Id，设置该教师所教的科目Id
     * @param {String} teacherId
     * @return 成功返回1，失败返回-1
     * @author: 姜东浩
     */
    public int setTeacherNewCourseId(Course course) {
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存教师的文件不为空，则反序列化后传给s里的AllTeacherList，否则返回-1
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                s.setAllTeacherList((ArrayList<Teacher>) ois.readObject());
            } else {
                return -1;
            }
            // 遍历教师列表，找到后设置所教科目Id，返回1，没找到返回-1
            for (int i = 0; i < s.getAllTeacherList().size(); i++) {
                if (s.getAllTeacherList().get(i).getTeacherId().equals(course.getcTeacher().getTeacherId())) {
                    s.getAllTeacherList().get(i).settCourseId(course.getCourseId());
                    oos = new ObjectOutputStream(new FileOutputStream(teacherData));
                    oos.reset();
                    oos.writeObject(s.getAllTeacherList());
                    return 1;
                }
            }
            return -1;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }

    }

    /**
     * @description: （老课，讲师删了，重新加讲师）通过教师Id和课程Id，设置该教师所教的科目Id
     * @param {String} teacherId
     * @return 成功返回1，失败返回-1
     * @author: 姜东浩
     */
    public int setTeacherNewCourseId(Course course, Teacher teacher) {
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存教师的文件不为空，则反序列化后传给s里的AllTeacherList，否则返回-1
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                s.setAllTeacherList((ArrayList<Teacher>) ois.readObject());
            } else {
                return -1;
            }
            // 遍历教师列表，找到后设置所教科目Id，返回1，没找到返回-1
            for (int i = 0; i < s.getAllTeacherList().size(); i++) {
                if (s.getAllTeacherList().get(i).getTeacherId().equals(teacher.getTeacherId())) {
                    s.getAllTeacherList().get(i).settCourseId(course.getCourseId());
                    oos = new ObjectOutputStream(new FileOutputStream(teacherData));
                    oos.reset();
                    oos.writeObject(s.getAllTeacherList());
                    return 1;
                }
            }
            return -1;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }

    }

    /**
     * @description: 移除该教师所教的科目Id
     * @param {String} teacherId
     * @return 成功返回1，没有教师数据库-1，没找到此教师返回-2，异常返回-3
     * @author: 姜东浩
     */
    public int removeTeacherNewCourseId(String teacherId) {
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存教师的文件不为空，则反序列化后传给s里的AllTeacherList，否则返回-1
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                s.setAllTeacherList((ArrayList<Teacher>) ois.readObject());
            } else {
                return -1; // 没有教师数据库，不用删
            }
            // 遍历教师列表，找到后设置所教科目Id，返回1，没找到返回-1
            for (int i = 0; i < s.getAllTeacherList().size(); i++) {
                if (s.getAllTeacherList().get(i).getTeacherId().equals(teacherId)) {
                    s.getAllTeacherList().get(i).settCourseId(null);
                    oos = new ObjectOutputStream(new FileOutputStream(teacherData));
                    oos.reset();
                    oos.writeObject(s.getAllTeacherList());
                    return 1;
                }
            }
            return -2; // 教师列表没找到此教师
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -3; // 异常返回-3
        } catch (IOException e) {
            e.printStackTrace();
            return -3; // 异常返回-3
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }

    }

    /**
     * @description: 移除该教师所管理的学生List
     * @param {String} teacherId
     * @return 成功返回1，没有教师数据库-1，没找到此教师返回-2，异常返回-3
     * @author: 姜东浩
     */
    public int removeTStudentList(String teacherId) {
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存教师的文件不为空，则反序列化后传给s里的AllTeacherList，否则返回-1
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                s.setAllTeacherList((ArrayList<Teacher>) ois.readObject());
            } else {
                return -1; // 没有教师数据库，不用删
            }
            // 遍历教师列表，找到后设置所教科目Id，返回1，没找到返回-1
            for (int i = 0; i < s.getAllTeacherList().size(); i++) {
                if (s.getAllTeacherList().get(i).getTeacherId().equals(teacherId)) {
                    s.getAllTeacherList().get(i).getStudentList().clear();
                    oos = new ObjectOutputStream(new FileOutputStream(teacherData));
                    oos.reset();
                    oos.writeObject(s.getAllTeacherList());
                    return 1;
                }
            }
            return -2; // 教师列表没找到此教师
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -3; // 异常返回-3
        } catch (IOException e) {
            e.printStackTrace();
            return -3; // 异常返回-3
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }

    }

    /**
     * @description: 向教师管理的学生列表添加学生
     * @param {Teacher} teacher
     * @param {ArrayList<Student>} studentList
     * @return 添加成功返回1，没有教师数据库返回-1，教师列表没找到此教师返回-2，抛异常返回-3
     * @author: 姜东浩
     */    
    public int addStudentListToTeacher(Teacher teacher, ArrayList<Student> studentList) {
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存教师的文件不为空，则反序列化后传给s里的AllTeacherList，否则返回-1
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                s.setAllTeacherList((ArrayList<Teacher>) ois.readObject());
            } else {
                return -1; // 没有教师数据库
            }
            // 遍历教师列表，找到后设置所教科目Id，返回1，没找到返回-1
            for (int i = 0; i < s.getAllTeacherList().size(); i++) {
                if (s.getAllTeacherList().get(i).getTeacherId().equals(teacher.getTeacherId())) {
                    s.getAllTeacherList().get(i).setStudentList(studentList);
                    oos = new ObjectOutputStream(new FileOutputStream(teacherData));
                    oos.reset();
                    oos.writeObject(s.getAllTeacherList());
                    return 1;
                }
            }
            return -2; // 教师列表没找到此教师
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -3; // 异常返回-3
        } catch (IOException e) {
            e.printStackTrace();
            return -3; // 异常返回-3
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }
    }

    /**
     * @description: 修改教师密码
     * @param {Long}   teacherId
     * @param {String} password
     * @return 成功返回1，失败返回-1
     * @author: 姜东浩
     */
    public int changeTeacherPwd(String teacherId, String password) {

        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存教师的文件不为空，则反序列化后传给s里的AllTeacherList，否则返回-1
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                s.setAllTeacherList((ArrayList<Teacher>) ois.readObject());
            } else {
                return -1;
            }
            // 遍历教师列表，找到后修改密码，再把更新完的List序列化到文件，返回1，没找到返回-1
            for (int i = 0; i < s.getAllTeacherList().size(); i++) {
                if (s.getAllTeacherList().get(i).getTeacherId().equals(teacherId)) {
                    s.getAllTeacherList().get(i).setPassword(password);
                    oos = new ObjectOutputStream(new FileOutputStream(teacherData));
                    oos.reset();
                    oos.writeObject(s.getAllTeacherList());
                    return 1;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }
        return -1;

    }

    /**
     * @description: 查看所有教师列表
     * @return 教师列表
     * @author: 姜东浩
     */
    public ArrayList<Teacher> showALlTeachers() {

        ObjectInputStream ois = null;
        // 如果存教师的文件不为空，则反序列化后传给s里的AllTeacherList，否则新建一个List传给s
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                s.setAllTeacherList((ArrayList<Teacher>) ois.readObject());
            } else {
                s.setAllTeacherList(new ArrayList<Teacher>());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }
        return s.getAllTeacherList();

    }

    // ========================================================================================================================
    /**
     * @description: 通过课程编号判断是否有此课程
     * @param {String} courseId
     * @return 有返回课程对象，没有返回null
     * @author: 姜东浩
     */
    public Course existCourseById(String courseId) {

        ObjectInputStream ois = null;
        // 如果存课程的文件不为空，则反序列化后传给s里的AllCourseList，否则返回null
        try {
            if (courseData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(courseData));
                s.setAllCourseList((ArrayList<Course>) ois.readObject());
            } else {
                return null;
            }
            // 遍历List，找到后返回对象，否则返回null
            for (int i = 0; i < s.getAllCourseList().size(); i++) {
                if (s.getAllCourseList().get(i).getCourseId().equals(courseId)) {
                    return s.getAllCourseList().get(i);
                }
            }
            return null;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }

    }

    /**
     * @description: 判断此课程是否有讲师
     * @param {String} courseId
     * @return 1有人删，-1没人删，-2有异常
     * @author: 姜东浩
     */
    public int existCourseHasTeacher(Course course) {

        ObjectInputStream ois = null;
        // 如果存课程的文件不为空，则反序列化后传给s里的AllCourseList，否则返回null
        try {
            if (courseData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(courseData));
                s.setAllCourseList((ArrayList<Course>) ois.readObject());
            } else {
                return -2;
            }
            // 遍历List，找到判断是否有人上这门课
            for (int i = 0; i < s.getAllCourseList().size(); i++) {
                if (s.getAllCourseList().get(i).getCourseId().equals(course.getCourseId())) {
                    if(s.getAllCourseList().get(i).getcTeacher()!=null){
                        return 1; // 有人上
                    }else{
                        return -1; // 没人上
                    }
                }
            }
            return -2;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -2;
        } catch (IOException e) {
            e.printStackTrace();
            return -2;
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }

    }

    /**
     * @description: 在课程总表添加课程
     * @param {Course} course
     * @return 成功返回1，失败返回-1
     * @author: 姜东浩
     */
    public int insertOneCourseInACL(Course course) {

        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存课程的文件不为空，则反序列化后传给s里的setAllCourseList，否则新建一个List传给s
        try {
            if (courseData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(courseData));
                s.setAllCourseList((ArrayList<Course>) ois.readObject());
            } else {
                s.setAllCourseList(new ArrayList<Course>());
            }
            // 向List集合添加一条数据，序列化到文件中，成功返回1，失败返回-1
            s.getAllCourseList().add(course);
            oos = new ObjectOutputStream(new FileOutputStream(courseData));
            oos.reset();
            oos.writeObject(s.getAllCourseList());
            return 1;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }

    }

    /**
     * @description: 删除课程
     * @param {Course} course
     * @return 删除成功返回1，没有课程数据库返回-1，没找到此课程返回-2，抛出异常返回-3
     * @author: 姜东浩
     */
    public int deleteOneCourse(Course course) {
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存课程的文件不为空，则反序列化后传给s里的AllCourseList，否则返回-1
        try {
            if (courseData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(courseData));
                s.setAllCourseList((ArrayList<Course>) ois.readObject());
            } else {
                return -1;
            }
            // 遍历课程列表，找到后删除，再把更新完的List序列化到文件，返回1，没找到返回-2，抛出异常返回-3
            for (int i = 0; i < s.getAllCourseList().size(); i++) {
                if (s.getAllCourseList().get(i).getCourseId().equals(course.getCourseId())) {
                    s.getAllCourseList().remove(i);
                    oos = new ObjectOutputStream(new FileOutputStream(courseData));
                    oos.reset();
                    oos.writeObject(s.getAllCourseList());
                    return 1;
                }
            }
            return -2;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -3;
        } catch (IOException e) {
            e.printStackTrace();
            return -3;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }

    }
    
    /**
     * @description: （因为删除了这名学生，所以他所学过的课程已报人数都减一）课程已报人数减一
     * @param {Student} students
     * @return 1操作成功，-1没有课程数据库，-3抛出异常
     * @author: 姜东浩
     */
    public int subtractOneInPsum(Student students) {
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存课程的文件不为空，则反序列化后传给s里的AllCourseList，否则返回-1
        try {
            if (courseData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(courseData));
                s.setAllCourseList((ArrayList<Course>) ois.readObject());
            } else {
                return -1;
            }
            // 遍历课程列表，找到此学生选过的所有课程后，已报人数减一
            for (int i = 0; i < students.getsCourseList().size(); i++) {
                for (int j = 0; j < s.getAllCourseList().size(); j++) {
                    if (students.getsCourseList().get(i).getCourseId()
                            .equals(s.getAllCourseList().get(j).getCourseId())) {
                        s.getAllCourseList().get(j).setCnum(s.getAllCourseList().get(j).getCnum() - 1);
                        break;
                    }
                }
            }
            oos = new ObjectOutputStream(new FileOutputStream(courseData));
            oos.reset();
            oos.writeObject(s.getAllCourseList());
            return 1;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -3;
        } catch (IOException e) {
            e.printStackTrace();
            return -3;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }
    }

    /**
     * @description: 给课程设置任课教师
     * @param {Course}  course
     * @param {Teacher} cTeacher
     * @return 成功返回1，失败-1
     * @author: 姜东浩
     */
    public int setCTeacherInCourse(Course course, Teacher cTeacher) {
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存课程的文件不为空，则反序列化后传给s里的setAllCourseList，否则新建一个List传给s
        try {
            if (courseData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(courseData));
                s.setAllCourseList((ArrayList<Course>) ois.readObject());
            } else {
                return -1;
            }
            // 遍历课程列表，找到对应课程，将该课程老师设置为传进来的老师
            for (int i = 0; i < s.getAllCourseList().size(); i++) {
                if (s.getAllCourseList().get(i).getCourseId().equals(course.getCourseId())) {
                    s.getAllCourseList().get(i).setcTeacher(cTeacher);
                    oos = new ObjectOutputStream(new FileOutputStream(courseData));
                    oos.reset();
                    oos.writeObject(s.getAllCourseList());
                    return 1;
                }
            }
            return -1;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }
    }

    /**
     * @description: 查看所有课程
     * @return 所有课程列表
     * @author: 姜东浩
     */
    public ArrayList<Course> showALlCourses() {

        ObjectInputStream ois = null;
        // 如果存课程的文件不为空，则反序列化后传给s里的AllCourseList，否则新建一个List传给s
        try {
            if (courseData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(courseData));
                s.setAllCourseList((ArrayList<Course>) ois.readObject());
            } else {
                s.setAllCourseList(new ArrayList<Course>());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }
        return s.getAllCourseList();

    }

    // ========================================================================================================================
    /**
     * @description: 修改自己密码
     * @param {String} password
     * @return 修改成功返回1，失败返回-1
     * @author: 姜东浩
     */
    public int changeMyPwd(String password) {

        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存超级管理员的文件不为空，则更改超级管理员的密码，序列到文件中。成功返回1，失败返回-1。
        try {
            if (courseData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(courseData));
                s.setPassword(password);
                oos = new ObjectOutputStream(new FileOutputStream(courseData));
                oos.reset();
                oos.writeObject(s);
                return 1;
            } else {
                return -1;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }
        return -1;

    }

}